import numpy as np

from sklearn.decomposition import PCA
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.feature_selection import VarianceThreshold,SelectKBest,f_regression,chi2,RFE,SelectFromModel
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVR
from sklearn.ensemble import GradientBoostingClassifier

X=np.array([
    [0, 2, 0, 3],
    [0, 1, 4, 3],
    [0.1, 1, 1, 3]
],dtype=np.float64)
Y=np.array([1,2,1])

variance=VarianceThreshold(threshold=0.1)
print(variance)
variance.fit(X)
print(variance.transform(X))
print('-'*30)

sk1=SelectKBest(f_regression,k=2)
sk1.fit(X,Y)
print(sk1)
print(sk1.scores_)
print(sk1.transform(X))
print('-'*30)

sk2=SelectKBest(chi2,k=2)
sk2.fit(X,Y)
print(sk2)
print(sk2.scores_)
print(sk2.transform(X))
print('-'*30)

estimator=SVR(kernel='linear')
selector=RFE(estimator,2,step=1)
selector=selector.fit(X,Y)
print(selector.support_)
print(selector.n_features_)
print(selector.ranking_)
print(selector.transform(X))
print('-'*30)

X2=np.array([
    [ 5.1,  3.5,  1.4,  0.2],
    [ 4.9,  3. ,  1.4,  0.2],
    [ -6.2,  0.4,  5.4,  2.3],
    [ -5.9,  0. ,  5.1,  1.8]
],dtype=np.float64)
Y2=np.array([0,0,2,2])

estimator=LogisticRegression(penalty='l1',C=0.1)
sfm=SelectFromModel(estimator)
sfm.fit(X2, Y2)
print(sfm.transform(X2))
print('-'*30)

estimator=GradientBoostingClassifier()
sfm=SelectFromModel(estimator)
sfm.fit(X2,Y2)
print(sfm.transform(X2))
print('-'*30)

pca=PCA(n_components=2)
pca.fit(X2)
print(pca.mean_)
print(pca.components_)
print(pca.transform(X2))
print('-'*30)

lda=LinearDiscriminantAnalysis(n_components=2)
lda.fit(X2, Y2)
print(lda.transform(X2))
print(lda.coef_)
#print(lda.covariance_)
print('-'*30)

X=np.array([
    [-1, -1], 
    [-2, -1], 
    [-3, -2], 
    [1, 1], 
    [2, 1], 
    [3, 2]
])
Y=np.array([1,1,2,2,1,1])
clf=LinearDiscriminantAnalysis()
clf.fit(X, Y)
print(clf.predict([[-0.8,-1]]))
print(clf.coef_)
print(X.shape)